/**
 * 七鱼客服对接相关
 */
import { v4 as uuidv4 } from 'uuid';
import YSF from '@neysf/qiyu-web-sdk';
import { getLocations, setLocations } from '@/utils/storage'

let uid = getLocations('qiyu_uid');

function _initYSF() {
  /**
     * 第一个参数是 企业 appkey
     * 第二个参数是 配置参数
    */
  YSF.init('7d1d2db2bef979175a273a8a1b4b1a9b', {
    sessionInvite: false, // sessionInvite表示是否开启会话邀请设置
    hidden: 1 // hidden表示是否隐藏访客端默认入口
  }).then(ysf => {
      // ysf('open');
      _initUid();
  }).catch(error => {
      console.log('sdk加载失败---', error);
  });
}
_initYSF();

// 生成uid
function _initUid() {
  console.log('_initUid');
  if (!uid) {
    uid = uuidv4();
  }
  setLocations('qiyu_uid', uid);
  ysf('config', {
      uid: uid,
      name: '自助修改客户',
  });
}

export function setConfig(options = {}) {
  ysf('config', {
      uid: uid,
      ...options
  });
}

export function open(options = {}) {
  ysf('open', {
    uid: uid,
      ...options
  });
}